package com.caiyigo.baihua.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.caiyigo.baihua.Const.Dictionary;
import com.caiyigo.baihua.dto.DataGrid;
import com.caiyigo.baihua.dto.Pager;
import com.caiyigo.baihua.dto.Result;
import com.caiyigo.baihua.dto.ZhuanyeDto;
import com.caiyigo.baihua.dto.ZykmDto;
import com.caiyigo.baihua.model.Kaoshikemu;
import com.caiyigo.baihua.service.ZykmService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

/**
 * @author Administrator
 * 
 */
@Controller
@RequestMapping(value = "/business/zykm")
public class ZykmController {
	@Autowired
	ZykmService zykmService;

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public @ResponseBody String resList(
			@RequestParam(value = "id", required = false) Integer id,
			@RequestParam(value = "passId", required = false) Integer passId,
			@RequestParam(value = "type", required = false, defaultValue = "1") Integer type)
			throws JsonProcessingException {
		ObjectMapper objectMapper = new ObjectMapper();
		return objectMapper.writeValueAsString(zykmService.listBySeq());
	}

	@RequestMapping(value = "/tree", method = RequestMethod.POST)
	public @ResponseBody String tree(
			@RequestParam(value = "id", required = false) Integer id,
			@RequestParam(value = "passId", required = false) Integer passId,
			@RequestParam(value = "type", required = false, defaultValue = "1") Integer type)
			throws JsonProcessingException {

		ObjectMapper objectMapper = new ObjectMapper();
		return objectMapper.writeValueAsString(zykmService.getTree(id, type,
				passId));
	}

	@RequestMapping(value = "/select", method = RequestMethod.POST)
	public @ResponseBody DataGrid<ZykmDto> select(ZykmDto dto, Pager page) {
		List<Order> orders = new ArrayList<Order>();
		orders.add(Order.create(page.getSort(), page.getOrder()));
		PageList<ZykmDto> pageList = (PageList<ZykmDto>) zykmService
				.selectForCombox(dto,
						new PageBounds(page.getPage(), page.getRows(), orders,
								true));
		return new DataGrid<ZykmDto>(pageList.getPaginator().getTotalCount(),
				pageList.getPaginator().getPage(), pageList);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Result delete(
			@RequestParam(value = "id", required = true) Integer id) {

		return new Result(zykmService.deleteByPrimaryKey(id));

	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody Result add(@ModelAttribute("dto") ZykmDto dto) {

		return new Result(zykmService.insertSelective(dto) > 0);

	}

	// @RequestMapping(value = "/edit", method = RequestMethod.POST)
	// public @ResponseBody Result edit(@ModelAttribute("user") ZhuanyeDto dto)
	// {
	//
	// if (zhuanyeService.updateByPrimaryKeySelective(dto) > 0) {
	// return new Result();
	// } else {
	// return new Result("冻结失败");
	// }
	//
	// }

	// 科目选择器,主要排序该专业已有的科目
	@RequestMapping(value = "/selectKemu", method = RequestMethod.POST)
	public @ResponseBody DataGrid<Kaoshikemu> selectKemu(String zyid,
			Kaoshikemu dto, Pager page) {
		List<Order> orders = new ArrayList<Order>();
		orders.add(Order.create(page.getSort(), page.getOrder()));
		PageList<Kaoshikemu> pageList = (PageList<Kaoshikemu>) zykmService
				.selectKemu(zyid, dto,
						new PageBounds(page.getPage(), page.getRows(), orders,
								true));
		return new DataGrid<Kaoshikemu>(
				pageList.getPaginator().getTotalCount(), pageList
						.getPaginator().getPage(), pageList);

	}

	// 科目选择器,按专业查询
	@RequestMapping(value = "/selectKemuByZyid", method = RequestMethod.POST)
	public @ResponseBody DataGrid<ZykmDto> selectKemuByZyid(ZykmDto dto,Pager page) {
		List<Order> orders = new ArrayList<Order>();
		orders.add(Order.create(page.getSort(), page.getOrder()));
		PageList<ZykmDto> pageList = (PageList<ZykmDto>) zykmService
				.selectKemuByZyid(dto,
						new PageBounds(page.getPage(), page.getRows(), orders,
								true));
		if (pageList.size()>0) {
			for (ZykmDto zykmDto : pageList) {
				zykmDto.setKmlxMc(Dictionary.KMLX.getName(zykmDto.getKmlx()));
			}
		}
		return new DataGrid<ZykmDto>(
				pageList.getPaginator().getTotalCount(), pageList
						.getPaginator().getPage(), pageList);

	}

}
